package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"tem/dto/controllerDto"
	"tem/middlewares"
	"tem/model"
)

type Comment struct {
}

func (Comment) Find(c *gin.Context) {
	var requestBody controllerDto.CommentRequestBody
	middlewares.DefaultValidator(c, &requestBody)
	fmt.Println(requestBody.ID, requestBody.PageIndex, requestBody.PageSize)
	list, count, err := model.Comment{}.FindList(requestBody.ID, requestBody.PageIndex, requestBody.PageSize)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
	} else {
		c.JSON(200, gin.H{
			"code": 0,
			"body": map[string]any{
				"list":  list,
				"count": count,
			},
		})
	}
}

func (Comment) Create(c *gin.Context) {
	var requestData model.Comment
	c.ShouldBind(&requestData)
	temp, _ := c.Get("id")
	requestData.UserId = uint(temp.(int))
	tx := model.Comment{}.Create(requestData)
	if tx.RowsAffected == 1 {
		c.JSON(200, gin.H{
			"code": 0,
			"msg":  "评论成功",
		})
	} else {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  "评论失败",
		})
	}
}
